Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try: Set show_in_rest to true by default when label argument is defined #7302

Conversation

SantosGuillamot
Copy link

@SantosGuillamot SantosGuillamot commented Sep 6, 2024

This pull request is built on top of #7298

As suggested by @gziolo here, in this pull request I'm exploring the possibility of setting the show_in_rest argument to true by default when the new potential label argument is defined. With the introduction of a new human-readable argument, which hasn't been used before, it could make sense to expose it automatically in the REST API.

Basically, these are the use cases:

  • If show_in_rest is defined, that value is always respected.
  • No label and no show_in_rest: It keeps working as before and uses show_in_rest false, which is the default. This is a pretty common use case right now.
  • Defined label and no show_in_rest: It sets show_in_rest to true. This would be the change introduced.

It seems I can't compare against my other branch. The relevant code changes in this PR are:

  • New conditional to change show_in_rest: link.
  • Add a new unit test to cover this use case: link.

Trac ticket: https://core.trac.wordpress.org/ticket/62000


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@@ -1451,6 +1454,9 @@ function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
* @param string $meta_key Meta key.
*/
$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
if ( isset( $args['label'] ) && ! isset( $args['show_in_rest'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should change before the filter so 3rd party code can still change it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when the filter adds a label without setting show_in_rest? Shouldn't that follow the same behavior?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is safer to move it before the filter as you say to ensure people can change this behavior.

@Mamaduka
Copy link
Member

Mamaduka commented Sep 6, 2024

This probably needs a separate track ticket. Similar discussions usually happen there.

@gziolo
Copy link
Member

gziolo commented Sep 6, 2024

I left one minor note. I'm in favor of that enhancement as it is the feedback we get several times from folks using Block Bindings that they need to remember to enable show_in_rest, but I'm happy to hear about potential consequences that make it a bad idea. I can't think of any as of today.

Copy link

github-actions bot commented Sep 6, 2024

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@SantosGuillamot
Copy link
Author

This probably needs a separate track ticket. Similar discussions usually happen there.

I've opened a separate ticket for this: https://core.trac.wordpress.org/ticket/62000#ticket.

@TimothyBJacobs
Copy link
Member

I'm pretty strongly against this. Exposing a meta value can be a pretty big footgun. I don't see a compelling reason why we should make that footgun less obvious by conflating the "label" field with whether the meta value is exposed.

Comment on lines +1457 to +1459
if ( isset( $args['label'] ) && ! isset( $args['show_in_rest'] ) ) {
$args['show_in_rest'] = true;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( isset( $args['label'] ) && ! isset( $args['show_in_rest'] ) ) {
$args['show_in_rest'] = true;
}
if ( isset( $args['label'] ) && ! isset( $args['show_in_rest'] ) ) {
$args['show_in_rest'] = true;
}

Just adding some spacing for clarity.

@dlh01
Copy link

dlh01 commented Sep 9, 2024

I'm sympathetic to the fact that it's easy to forget to enable show_in_rest in lots of situations, but I agree with @TimothyBJacobs. As described in #7298, the purpose of the label is to have "a human-readable name that can be used across the UI" (which is great!). As a developer, I would have no expectation that taking advantage of that enhancement would potentially affect whether data is exposed to the outside world. Even setting a post type to public doesn't automatically enable show_in_rest, and that seems like as strong an indicator as could have been. So, for me, this would be an unwelcome surprise.

@gziolo
Copy link
Member

gziolo commented Sep 9, 2024

Thank you for the feedback shared so far. In retrospect, I'm not surprised as it indeed could be confusing to learn that label has some special behavior when registering a new meta. It looks like what could help is having something at the level of abstraction that WP_REST_Meta_Fields is. In essence, is there a way we could allow registering a meta field, that with some new set of properties could get easily integrated into the editor and new concepts like Block Bindings, Data Views, exposed in the sidebar of the editor when editing the post type, etc. I guess the Fields API project is partially related to that but in other areas of WP Admin.

@TimothyBJacobs
Copy link
Member

is there a way we could allow registering a meta field, that with some new set of properties could get easily integrated into the editor and new concepts like Block Bindings, Data Views, exposed in the sidebar of the editor when editing the post type, etc

Isn't that what register_meta_field and the REST API does? What am I missing?

@gziolo
Copy link
Member

gziolo commented Sep 11, 2024

Isn't that what register_meta_field and the REST API does? What am I missing?

There is no function register_meta_field, so I assume you are referring to how the REST API works for Post Meta. At some point, we will have to revisit it and introduce some more high level utility that while registering post meta field is able to autoamtically expose it through REST API, create a block variations with ease for existing blocks with bindings applied, etc.

Anyway, I'm closing this PR for now. Thank you all for valuable feedback.

@gziolo gziolo closed this Sep 11, 2024
@Mamaduka
Copy link
Member

I think @TimothyBJacobs was referring to the register_rest_field method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants